package AlibabaProductTitleSniffing;

import AlibabaProductTitleSniffing.Utils.DataConnection;
import JAVAFXManyWindow.impl.GetPostDat;
import JAVAFXManyWindow.pojo.MainMe;
import com.domain.ClassBox;
import com.mapper.ClassBoxMapper;
import org.apache.ibatis.session.SqlSession;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 * @author 10125
 */
public class App
{
    public static void main( String[] args )
    {
        getclassOne();

//        List<String> html = new ArrayList<>();
//        html.add("index.html");
//        int ispro = MainMe.jxBrowserServerTcp.initcssjs(html,null,null);
//        if(ispro>0){
//            MainMe.jxBrowserServerTcp.GetJXBrowserServerTcp(MainMe.jxBrowserServerTcp.MacAddressIP4(), 1024, "#eeeeee", new GetPostDat() {
//                @Override
//                public void dat(List<String> list, String s, OutputStream outputStream) {
//                    try {
//                        synchronized (App.class){
//
//                        }
//                    }catch (Exception e){e.printStackTrace();}
//                }
//
//                @Override
//                public void addressUrl(String s) {
//                    try {
//                        synchronized (App.class){
//
//                        }
//                    }catch (Exception e){e.printStackTrace();}
//                }
//            });
//            MainMe.jxBrowserServerTcp.AddressURL(MainMe.jxBrowserServerTcp.setscanUHttp(1024)+"index.html");
//        }
    }
    /**
     * 复用类
     * */
    static Elements bos(Document document,int z){
        try {
            Elements elements =document.getElementsByClass("drop-list fd-hide");
            Element frame = elements.get(z);
            Elements ul = frame.select("ul");
            Elements li = ul.select("li");
            Elements a = li.select("a");
            return a;
        }catch (Exception e){return null;}
    }
    static Elements thres(Document document){
        try {
            Elements elements =document.getElementsByClass("drop-list short-drop-list fd-hide");
            Element frame = elements.get(0);
            Elements ul = frame.select("ul");
            Elements li = ul.select("li");
            Elements a = li.select("a");
            return a;
        }catch (Exception e){return null;}
    }

    /**
     * 第一类
     * @return List<String>
     * */
     static void   getclassOne(){
        try {
//            DataConnection dataConnection =new DataConnection();
//            SqlSession sqlSession = dataConnection.getSqlsession();
//            ClassBoxMapper classIsdMapper = sqlSession.getMapper(ClassBoxMapper.class);
//            ClassBox classIsd = new ClassBox();
            Document document = Jsoup.connect("http://index.1688.com/alizs/keyword.htm").timeout(200000).get();
            String nulls = "";
            for (Element s:bos(document,1)) {
                boolean title = s.attr("title").equals(nulls);
                boolean key = s.attr("data-key").equals(nulls);
                if(!key && !title){
                    String ids = s.attr("data-key");
                    System.err.println(ids);
//                    classIsd.setClassUids(ids);
//                    classIsd.setClassTitle(s.attr("title"));
//                    classIsdMapper.insertSelective(classIsd);
//                    sqlSession.commit();
                    Document ont = Jsoup.connect("http://index.1688.com/alizs/keyword.htm?cat="+ids).timeout(200000).get();
                    Elements asflag = bos(ont,2);
                    if(asflag!=null){
                        for (Element onts:asflag) {
                            boolean ontstitle = onts.attr("title").equals(nulls);
                            boolean ontskey = onts.attr("data-key").equals(nulls);
                            if(!ontskey && !ontstitle){
                                String bid = s.attr("data-key")+","+onts.attr("data-key");
                                Document outMe = Jsoup.connect("http://index.1688.com/alizs/keyword.htm?cat="+bid).timeout(200000).get();
                                Elements outMeflag = thres(outMe);
                                if(outMeflag!=null){
                                    for (Element outMes:outMeflag) {
                                        boolean outMetitle = onts.attr("title").equals(nulls);
                                        boolean outMekey = onts.attr("data-key").equals(nulls);
                                        if(!outMekey && !outMetitle){
                                            String vas = s.attr("data-key")+","+onts.attr("data-key")+","+outMes.attr("data-key");
                                            System.err.println(vas);
//                                            classIsd.setClassUids(vas);
//                                            classIsd.setClassTitle(s.attr("title")+" | "+onts.attr("title")+" | "+outMes.attr("title"));
//                                            classIsdMapper.insertSelective(classIsd);
//                                            sqlSession.commit();
                                        }
                                    }
                                }else{
                                    String vas = s.attr("data-key")+","+onts.attr("data-key");
                                    System.err.println(vas);
//                                    classIsd.setClassUids(vas);
//                                    classIsd.setClassTitle(s.attr("title")+" | "+onts.attr("title"));
//                                    classIsdMapper.insertSelective(classIsd);
//                                    sqlSession.commit();
                                }
                            }
                        }
                    }
                }
            }
            return;
        }catch (Exception e){e.printStackTrace();}
         return;
    }
}
